#include "netlib.h"

#include "socket.h"
#include "netaddress.h"
#include "accepter.h"
#include "accepterhandler.h"
#include "sockethandler.h"
#include "buffer.h"

namespace kxlua {

NetLib::NetLib(Lua& lua) :lua_(lua) {
    AddClass(new NetAddress(this));
    AddClass(new Socket(this));
    AddClass(new SocketHandler(this));
    AddClass(new Accepter(this));
    AddClass(new AccepterHandler(this));
    AddClass(new Buffer(this));
}

NetLib::~NetLib() {
    for (Classes::iterator it = classes.begin(); it != classes.end(); it++) {
        delete it->second;
    }
}

Lua& NetLib::GetLua() {
    return lua_;
}

void NetLib::AddClass(IClass* c) {
    classes.insert(std::make_pair(c->GetName(), c));
}

std::string NetLib::GetName() const {
    return "net";
}

IClass* NetLib::GetClass(std::string name) const {
    Classes::const_iterator it = classes.find(name);
    if (it != classes.end()) {
        return it->second;
    }
    return NULL;
}

bool NetLib::Open() {
    bool ret = true;
    for (Classes::iterator it = classes.begin(); it != classes.end(); it++) {
        ret && (ret = it->second->Open());
    }
    return ret;
}

bool NetLib::Close() {

}

}
